package io.gitee.yxsnake.mybatis.plus.configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import io.gitee.yxsnake.mybatis.plus.configuration.properties.MybatisPlusExtProperties;
import io.gitee.yxsnake.mybatis.plus.handler.MybatisExceptionHandler;
import io.gitee.yxsnake.mybatis.plus.interceptor.DataDecryptInterceptor;
import io.gitee.yxsnake.mybatis.plus.interceptor.DataEncryptInterceptor;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author snake
 * @description
 * @since 2024/10/15 23:46
 */
@Slf4j
@Import({MybatisPlusExtProperties.class,})
@Configuration
@RequiredArgsConstructor
public class CustomMybatisPlusAutoConfiguration {

    private final MybatisPlusExtProperties mybatisPlusExtProperties;


    @Value("${sm4.key:e5153bb7469a8edc1853cc0fc6cd0e08}")
    private String sm4Key;

    @PostConstruct
    public void init() {
        log.info("------------ mybatis-plus-starter StartUp Information -----------");
        log.info("mybatis-plus-starter");
        log.info("-------------------------------------------------------------");
    }
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加非法SQL拦截器
        if(mybatisPlusExtProperties.getIllegalEnabled()){
            interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
        }
        // 防全表更新删除操作
        if(mybatisPlusExtProperties.getBlockAttackEnabled()){
            interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        }
        // 增加@Version乐观锁支持
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        // 配置分页插件 放在最后
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.getDbType(mybatisPlusExtProperties.getDbType()));
        paginationInnerInterceptor.setMaxLimit(mybatisPlusExtProperties.getMaxLimit());
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }



    /**
     * 异常处理器
     */
    @Bean
    public MybatisExceptionHandler mybatisExceptionHandler() {
        return new MybatisExceptionHandler();
    }

    /**
     * 字段加密拦截器
     * @return
     */
    @Bean
    public DataEncryptInterceptor encryptInterceptor(){
        return new DataEncryptInterceptor(mybatisPlusExtProperties,sm4Key);
    }

    /**
     * 字段解密拦截器
     * @return
     */
    @Bean
    public DataDecryptInterceptor decryptInterceptor(){
        return new DataDecryptInterceptor(mybatisPlusExtProperties,sm4Key);
    }

}
